<template>
  <div class="warp">
    <van-nav-bar
      title="商品列表"
      left-text="返回"
      right-text
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <div class="title">
      <div class="login_title">
        <p :style="{color: currentColor}">{{ title }}</p>
      </div>
    </div>
    <van-card
      :price="item.price"
      :title="item.name"
      :thumb="item.coverImg"
      v-for="(item,index) in hotshops"
      :key="index"
      @click="details(item)"
    >
      <template #footer>
        <van-button size="mini">购买</van-button>
      </template>
    </van-card>
    <!-- 自动下一页 -->
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell v-for="item in list" :key="item" :title="item" />
    </van-list>
  </div>
</template>

<script>
import { hotShop } from "../services/dress";
export default {
  data() {
    return {
      title: "",
      loading: false,
      finished: false,
      list: [],
      hot: "",
      hotshops: [],
      num: 1,
      currentColor: "",
    };
  },
  created() {
    hotShop(this.$store.state.sortListUrl).then((res) => {
      this.hotAll = res.data.products;
      this.title = res.data.products[0].productCategory.name;
    });
    let r = parseInt(Math.random() * 256);
    let g = parseInt(Math.random() * 256);
    let b = parseInt(Math.random() * 256);
    console.log(`rgba(${r},${g},${b})`);
    this.currentColor = `rgba(${r},${g},${b})`;
  },
  methods: {
    onClickLeft() {
      history.go(-1);
    },
    onClickRight() {},
    reduceOne() {
      if (this.num > 1) {
        this.num = Number(this.num) - 1;
      }
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        this.num++;
        hotShop(this.$store.state.sortListUrl, this.num).then((res) => {
          for (let i = 0; i < res.data.products.length; i++) {
            this.hotshops.push(res.data.products[i]);
          }
        });

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 40) {
          this.finished = true;
        }
      }, 1000);
    },
    back() {
      this.$router.push({
        name: "/Home",
      });
      location.reload();
    },
    details(item) {
      this.$store.commit("toDetails", item);
      this.$router.push({
        name: "Details",
      });
    },
  },
};
</script>

<style>
.wrap {
  margin-top: 30px;
  display: flex;
}
.title {
  margin-top: 50px;
}
.jiantou {
  width: 100%;
  height: 50px;
  background: #f8fbf8;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 99999;
}
.back {
  font-size: 20px;
  height: 25px;
  width: 26px;
  margin: 0;
  margin-left: 20px;
  background: #cccccc;
  border-radius: 50%;
  margin-top: 10px;
  padding-left: 4px;
  padding-top: 6px;
}
.login_title {
  height: 70px;
  background: url(https://s1.ax1x.com/2020/08/27/dhu9yQ.png);
  background-size: 150% 70px;
  margin-bottom: 20px;
  position: relative;
}
.login_title p {
  position: absolute;
  left: 50%;
  bottom: 3px;
  font-size: 18px;
  line-height: 0;
}
</style>